package com.example.droideep.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.droideep.R;
import com.example.droideep.common.activities.SimpleFragmentBase;
import com.example.droideep.common.dummydata.Cheeses;

import java.util.List;

import butterknife.InjectView;

/**
 * A fragment representing a list of Items.
 * <p/>
 * <p/>
 * Activities containing this fragment MUST implement the {@link }
 * interface.
 */
public class PagingFragment extends SimpleFragmentBase implements AbsListView.OnScrollListener {

    @InjectView(android.R.id.list)
    ListView listView;

    ProgressBar progressBar;
    private boolean loading = false;

    private int page = 1;
    private final int size = 10;

    private final List<String> dummyContents = Cheeses.asList();

    private ArrayAdapter<String> dummyAdapter;

    @Override
    protected int getContentResId() {
        return R.layout.fragment_paging;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);

        progressBar = new ProgressBar(getActivity());
//        Button button = new Button(getActivity());
//        button.setText("Hello");
        listView.addFooterView(progressBar);
        listView.setOnScrollListener(this);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView.setAdapter(dummyAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1));
    }

    @Override
    public void onResume() {
        super.onResume();
        new DelayAsyncTask().execute();
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (!(loading) && (totalItemCount - visibleItemCount) <= (firstVisibleItem)) {
            Toast.makeText(getActivity(), "Load Next Page", Toast.LENGTH_SHORT).show();
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (loading) {
            new DelayAsyncTask().execute();
        }
    }

    private class DelayAsyncTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).run();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            List<String> subList = dummyContents.subList(((page * size) - size), (page * size) - 1);
            dummyAdapter.addAll(subList);
            loading = false;

        }
    }
}
